/**
XenSim simulates xenbal.
Copyright (C) 2006 Travis Vachon

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/
package xensim;

import java.util.Iterator;

public class MigrationQuantumEvent extends Event {
	TransferDaemon daemon;
	
	/**
	 * This will be set by the Connection.request_xBandwidth() method. A little funky, but ok. 
	 */
	long usableBandwidth;

	public MigrationQuantumEvent(double executionTime, TransferDaemon daemon) {
		super(executionTime);
		this.daemon = daemon;
		this.usableBandwidth = 0;
	}

	@Override
	public void execute() {
//		if(SimGlobals.DEBUG){
//			System.out.println("Migration Quantum: " +usableBandwidth+"," +daemon.memoryToTransfer);
			
//		}
		//System.out.println(daemon.memoryToTransfer + " - " + usableBandwidth);
		daemon.memoryToTransfer -= usableBandwidth; //Bandwidth in bits/millisecond
		
		Iterator<Connection.ERPair> i = daemon.source.connection.txMigrationEvents.iterator();
		while(i.hasNext()){
			if(i.next().event == this){ 
				i.remove();
				break;
			}
		}
		
		i = daemon.destination.connection.rxMigrationEvents.iterator();
		while(i.hasNext()){
			if(i.next().event == this){ 
				i.remove();
				break;
			}
		}

		
		if (daemon.memoryToTransfer <= 0) {
			daemon.migrationIterationFinish();
			
		}
		else daemon.migrationQuantum();
		
	}

}
